package com.example.administrator.mygdmap;

import android.content.Context;
import android.content.pm.ProviderInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.maps2d.overlay.BusRouteOverlay;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.district.DistrictResult;
import com.amap.api.services.district.DistrictSearch;
import com.amap.api.services.district.DistrictSearchQuery;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,PoiSearch.OnPoiSearchListener,Inputtips.InputtipsListener,TextWatcher{
    //增加一些东西

    //zengjia222
    private Button btn_1,btn_2,btn_3;
    private MapView mapView;
    private AMap aMap;
    private Context mContext;

    private int a2;

    private AutoCompleteTextView ed_select;
    private ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        aMap = mapView.getMap();
//        MyLocationStyle myLocationStyle;
//        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
//        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
//        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
//        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
//        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
//        myLocationStyle.showMyLocation(true);
//        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;

        DistrictSearch search = new DistrictSearch(this);
        DistrictSearchQuery query = new DistrictSearchQuery();
        query.setKeywords("北京");//传入关键字
        query.setShowBoundary(true);//是否返回边界值
        search.setQuery(query);
        search.setOnDistrictSearchListener(new DistrictSearch.OnDistrictSearchListener() {
            @Override
            public void onDistrictSearched(DistrictResult districtResult) {

            }
        });//绑定监听器
        search.searchDistrictAnsy();//开始搜索

        init();
//        get();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
       get(s.toString(),"北京");
    }

    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        Toast.makeText(mContext,"look"+ list.get(1).getName(),Toast.LENGTH_SHORT).show();
        List<String> sl = new ArrayList<String>();
        for (int k = 0;k<list.size();k++){
            sl.add(list.get(k).getName());
        }
       // adapter.clear();
        adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,sl);
        ed_select.setAdapter(adapter);

    }

    private void getPoi(){
        PoiSearch.Query myquery = new PoiSearch.Query("肯德基","","北京");
        myquery.setPageSize(10);
        myquery.setPageNum(2);

        PoiSearch poi = new PoiSearch(this,myquery);
        poi.setOnPoiSearchListener(this);
        poi.searchPOIAsyn();
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        ArrayList<PoiItem> poi = poiResult.getPois();
        Toast.makeText(mContext,"OK"+ poi.get(0).getProvinceCode(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        Toast.makeText(mContext,"OK",Toast.LENGTH_SHORT).show();
    }

    private void init(){
        btn_1 = (Button)findViewById(R.id.btn_1);
        btn_2 = (Button)findViewById(R.id.btn_2);
        btn_3 = (Button)findViewById(R.id.btn_3);

        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);

        ed_select = (AutoCompleteTextView)findViewById(R.id.ed_select);
        ed_select.addTextChangedListener(this);
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_1:
                aMap.setMapType(1);
                break;
            case R.id.btn_2:
                aMap.setMapType(2);
                break;
            case R.id.btn_3:
                getPoi();
                break;
        }
    }

    private void get(String ed,String city){
        //Toast.makeText(mContext,"look:"+ ed + "  " + city,Toast.LENGTH_SHORT).show();
//        InputtipsQuery in = new InputtipsQuery("北京","北京");
        InputtipsQuery in = new InputtipsQuery(ed,city);
        in.setCityLimit(true);
        Inputtips inputtips = new Inputtips(MainActivity.this,in);
        inputtips.setInputtipsListener(this);
        inputtips.requestInputtipsAsyn();
    }

}

